package fule.com.playandroidkotlin.ui.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import fule.com.playandroidkotlin.base.BaseActivity
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.BaseModel
import fule.com.playandroidkotlin.ui.model.LoginModel
import fule.com.playandroidkotlin.ui.presenter.LoginPresenter
import fule.com.playandroidkotlin.ui.view.LoginView
import kotlinx.android.synthetic.main.activity_login.*

/**
 * 作者： njb
 * 时间： 2018/10/26 0026-下午 4:36
 * 描述：
 * 来源：
 */
class LoginActivity : BaseActivity<LoginPresenter>(), LoginView, View.OnClickListener {


    override val layoutId: Int
        get() = R.layout.activity_login

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter(this)
    }

    override fun initView() {

    }

    override fun addListener() {
        btn_login.setOnClickListener(this)
        tv_sign_up.setOnClickListener(this)
    }

    override fun onLoadLogin(o: BaseModel<LoginModel>?) {
    }

    override fun showLoading() {

    }

    override fun hideLoading() {
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_login -> {
                login();
            }
            R.id.tv_sign_up -> {
                Intent(this@LoginActivity, LoginActivity::class.java).apply {
                    startActivity(this)
                }
                finish()
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }

        }
    }

    private fun login() {
        if (checkInput()) {
            presenter!!.login(et_username.text.toString(), et_password.text.toString())
        }
    }

    private fun checkInput(): Boolean {
        var isLogin = true
        val username = et_username.text.toString()
        val password = et_password.text.toString()
        if (TextUtils.isEmpty(username)) {
            et_username.error = (getString(R.string.tip_username_can_not_be_empty))
            isLogin = false
        }
        if (TextUtils.isEmpty(password)) {
            et_password.error = (getString(R.string.tip_password_can_not_be_empty))
        }
        return isLogin
    }


}